@extends('layouts._app')
@section('content')
    <body>
    <div class="wrap-container">
        <div class="column-content-detail">
            <div class="col-lg-12">
                <div class="email-content" style="overflow:auto; width:100%;">
                    {!! Form::open(['class'=>'layui-form']) !!}
                    <div class="layui-form-item">
                        <div class="layui-input-inline tool-btn">
                            <a class="layui-btn layui-btn-small layui-btn-primary" id='email-refresh-btn'
                               href="{{route('system.browse.index')}}?category={{$request['category']}}"
                               data-title='刷新'>
                                <i class="layui-icon">&#x1002;</i>
                            </a>
                            <button class="layui-btn layui-btn-small layui-btn-danger delBtn hidden-xs">
                                <i class="layui-icon">&#xe640;</i>
                            </button>
                            <button class="layui-btn layui-btn-small layui-btn-warm truncateBtn hidden-xs">
                                <i class="layui-icon">&#xe640;</i>
                            </button>
                        </div>
                        <div class="layui-input-inline">
                            <select id="change_category" lay-filter="category" lay-search>
                                @foreach(config('page.database') as $key=>$value)
                                    <option value="{{$key}}"
                                            @if($request['category'] == $key) selected @endif>{{$value}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="layui-inline" style="float: right;">
                            <select id="change_category1" lay-filter="category1" lay-search
                                    data-id="{{$request['category'] == 1 ? 'blog' : 'admin'}}">
                                <option selected>下载</option>
                                <option>.txt</option>
                                <option>.xlsx</option>
                            </select>
                        </div>
                    </div>
                    {!! Form::close() !!}
                    @include('layouts._flash')
                    <div class="layui-form" id="table-list">
                        <table class="layui-table">
                            <thead>
                            <tr>
                                <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                                <th>IP</th>
                                <th>资源类型</th>
                                <th>请求方法</th>
                                <th>是否ajax请求</th>
                                <th>路由</th>
                                <th>地址</th>
                                <th>浏览器</th>
                                <th>浏览器版本</th>
                                <th>操作系统</th>
                                <th>操作系统版本</th>
                                <th>浏览器语言</th>
                                <th>系统</th>
                                <th>类型</th>
                                <th>时间</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($browse as $value)
                                <tr>
                                    <td><input type="checkbox" name="del" lay-skin="primary" data-id="{{$value->id}}">
                                    </td>
                                    <td title="{{$value->ip}}">{{sub($value->ip)}}</td>
                                    <td title="{{$value->type}}">{{sub($value->type)}}</td>
                                    <td title="{{$value->method}}">{{sub($value->method)}}</td>
                                    <td title="{{$value->isAjax}}">{{sub($value->isAjax)}}</td>
                                    <td title="{{$value->module}}">{{sub($value->module)}}</td>
                                    <td title="{{$value->url}}">{{sub($value->url)}}</td>
                                    <td title="{{$value->browser}}">{{sub($value->browser)}}</td>
                                    <td title="{{$value->browser_version}}">{{sub($value->browser_version)}}</td>
                                    <td title="{{$value->platform}}">{{sub($value->platform)}}</td>
                                    <td title="{{$value->platform_version}}">{{sub($value->platform_version)}}</td>
                                    <td title="{{$value->lang}}">{{sub($value->lang)}}</td>
                                    <td title="{{$value->os}}">{{sub($value->os)}}</td>
                                    <td title="{{$value->mobile}}">{{sub($value->mobile)}}</td>
                                    <td title="{{$value->created_at}}">{{sub($value->created_at)}}</td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                        <!--tp分页-->
                        <div class="page-wrap">
                            <p style="float: left;">注 : 共 {{$browse->total()}} 条记录</p>
                            {{ $browse->links() }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="{{asset('static/admin/layui/layui.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('static/admin/js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script>
        layui.use(['form', 'dialog'], function () {
            var form = layui.form();
            var dialog = layui.dialog;
            form.on('select(category)', function (e) {
                window.location.href = "{{route('system.browse.index')}}?category=" + e.value;
            })
            //顶部批量删除
            $('.delBtn').click(function () {
                var ids = [];
                $("input[name='del']:checked").each(function (i) {
                    ids[i] = $(this).data('id');
                });
                if (ids.length == 0) {
                    layer.msg('请选择要删除的', {icon: 5, time: 3000});
                    return false;
                }
                var category = "{{$request['category']}}";
                dialog.confirm({
                    message: '您确定要删除选中项',
                    success: function () {
                        $.ajax({
                            type: 'POST',
                            url: '{{route('system.browse.destroy')}}',
                            data: {id: ids, filter: true, category: category},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
                return false;

            }).mouseenter(function () {

                dialog.tips('批量删除', '.delBtn');

            })
            //顶部批量删除
            $('.truncateBtn').click(function () {
                var category = "{{$request['category']}}";
                var name = 'admin';
                switch (category) {
                    case 2:
                        name = 'blog';
                        break;
                    default:
                        name = 'admin';
                        break;
                }
                dialog.confirm({
                    message: '您确定要清空？',
                    success: function () {
                        $.ajax({
                            type: 'POST',
                            url: '{{route('system.browse.truncate')}}',
                            data: {filter: true, truncate: name},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
                return false;

            }).mouseenter(function () {

                dialog.tips('清空', '.truncateBtn');

            })
            form.on('select(category1)', function (e) {
                if (e.value == '下载') {
                    return false;
                }
                /**
                 * 写入文件下载
                 * @param id
                 */
                var table = $("#change_category1").data('id');
                var ext = e.value;
                dialog.confirm({
                    message: '这可能会耗费很长时间！是否继续',
                    success: function () {
                        window.open('{{route('TextFile')}}' + '?id=' + table + '&ext=' + ext, '_parent');
                        window.location.reload();
                    },
                });
            });
            form.render();
        });
    </script>
    </body>
@endsection
